---
title: "Replication File"
author: "Aiden Parker and Jesse Usher-Barret"
description: "From Politics to the Bench Partisan Determinants of Support for Gubernatorial Judicial Appointments"
format:
  html:
    df-print: paged
    css: "styles.css"
    embed-resources: true
    self-contained-math: true
categories:
  - Project 
---


```{r, warning=FALSE, message=FALSE}
library(haven)
library(tidyverse)
library(stargazer)
library(forcats) 
library(knitr)
library(summarytools)
library(car)
library(sjPlot)
library(ggeffects)
library(sjlabelled)
library(sjmisc)
library(ggplot2)
library(glmtoolbox)
library(cobalt)
# library(rms)
library(AICcmodavg)
library(sandwich)


###load data
# modeldata <- read_sav("Spring24Survey.sav")


```

# Subsets and Transformations

## Changing Variable Types

```{r}
### subset
subsetmodeldata <-  modeldata %>% select(Control, GOP_Treatment, Dem_Treatment, pid3lean, gender, age, education, Q5_1, Q5_2, Q12_2, Q12_3, hhi, hispanic, ethnicity, region)

###change Variable Types

subsetmodeldata$hispanic <- as.numeric(subsetmodeldata$hispanic)
subsetmodeldata$ethnicity <- as.numeric(subsetmodeldata$ethnicity)
subsetmodeldata$Control <- as.factor(subsetmodeldata$Control)
subsetmodeldata$region <- as.factor(subsetmodeldata$region)
subsetmodeldata$Dem_Treatment <- as.factor(subsetmodeldata$Dem_Treatment)
subsetmodeldata$GOP_Treatment <- as.factor(subsetmodeldata$GOP_Treatment)
subsetmodeldata$age <- as.numeric(subsetmodeldata$age)
subsetmodeldata$education <- as.factor(subsetmodeldata$education)
subsetmodeldata$hhi <- as.factor(subsetmodeldata$hhi)
subsetmodeldata$hhi <- factor(subsetmodeldata$hhi, levels = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25))
subsetmodeldata$pid3lean <- as.factor(subsetmodeldata$pid3lean)


### Rename Variables
subsetmodeldata <- subsetmodeldata %>% rename(DemTherm = Q5_1,
                                               RepTherm = Q5_2,
                                               TrustDem = Q12_2,
                                               TrustRep = Q12_3)


```

## Eliminating Unusable Observations 

```{r}
subsetmodeldata <- subset(subsetmodeldata, as.character(Dem_Treatment) != "-99" | as.character(GOP_Treatment) != "-99" | as.character(Control) != "-99")

subsetmodeldata <- subset(subsetmodeldata, !(gender == "" & ethnicity == "")) 


subsetmodeldata <- subset(subsetmodeldata, !(ethnicity == 16 | hhi == 25 | pid3lean == "" | education == -3105 | hispanic == 15 | DemTherm == -99 | RepTherm == -99 | TrustDem == -99 | TrustRep == -99 ))

```

## Re-coding where necessary 

```{r}
#Recode hispanic variable
subsetmodeldata$hispanrecode <- ifelse(subsetmodeldata$hispanic == 1, 0,
                                   ifelse(subsetmodeldata$hispanic >= 2 & 
                                            subsetmodeldata$hispanic <= 16, 1, 
                                          subsetmodeldata$hispanic))


subsetmodeldata <- subsetmodeldata %>% mutate(hispanrecode, "Hispanic" = case_when(
  hispanrecode == 0 ~ "Non-Hispanic", 
  hispanrecode == 1 ~ "Hispanic"
))
  
subsetmodeldata$Hispanic <- factor(subsetmodeldata$Hispanic, levels = c("Non-Hispanic", "Hispanic"))


#Recode ethnicity variable
subsetmodeldata$ethnicrecode <- ifelse(subsetmodeldata$ethnicity >= 4 & 
                                         subsetmodeldata$ethnicity <= 10, 4,
                                       ifelse(subsetmodeldata$ethnicity >= 11 & 
                                                subsetmodeldata$ethnicity <= 14, 5,
                                              ifelse(subsetmodeldata$ethnicity == 15
                                                     , 6, subsetmodeldata$ethnicity
                                                     )))

subsetmodeldata <- subsetmodeldata %>% mutate(ethnicrecode, Ethnicity = case_when(
  ethnicrecode == 1 ~ "White",
  ethnicrecode == 2 ~ "African American", 
  ethnicrecode == 3 ~ "American Indian/Alaskan Native", 
  ethnicrecode == 4 ~ "Asian",
  ethnicrecode == 5 ~ "Pacific Islander",
  ethnicrecode == 6 ~ "Other"
))


subsetmodeldata$Ethnicity <- factor(subsetmodeldata$Ethnicity, levels = c("White", "Black", "American Indian/Alaskan Native", "Asian", "Pacific Islander", "Other"))

#create dummy variables indicating which treatment was received
subsetmodeldata$control_dummy <- ifelse(subsetmodeldata$Control %in% c(1, 2), 1, 0)
subsetmodeldata$Dem_dummy <- ifelse(subsetmodeldata$Dem_Treatment %in% c(1, 2), 1, 0)
subsetmodeldata$Dem_dummy <- as.factor(subsetmodeldata$Dem_dummy)

subsetmodeldata$GOP_dummy <- ifelse(subsetmodeldata$GOP_Treatment %in% c(1, 2), 1, 0)
subsetmodeldata$GOP_dummy <- as.factor(subsetmodeldata$GOP_dummy)

#recode household income variable
subsetmodeldata <- subsetmodeldata %>% mutate(hhi, HouseholdIncome = case_when(
  hhi == 1 ~ "Less than $14,999",
  hhi == 2 ~ "$15,000-$19,999",
  hhi == 3 ~ "$20,000-$24,999",
  hhi == 4 ~ "$25,000-$29,999",
  hhi == 5 ~ "$30,000-$34,999",
  hhi == 6 ~ "$35,000-$39,999",
  hhi == 7 ~ "$40,000-$44,999",
  hhi == 8 ~ "$45,000-$49,999",
  hhi == 9 ~ "$50,000-$54,999",
  hhi == 10 ~ "$55,000-$59,999",
  hhi == 11 ~ "$60,000-$64,999",
  hhi == 12 ~ "$65,000-$69,999",
  hhi == 13 ~ "$70,000-$74,999",
  hhi == 14 ~ "$75,000-$79,999",
  hhi == 15 ~ "$80,000-$84,999",
  hhi == 16 ~ "$85,000-$89,999",
  hhi == 17 ~ "$90,000-$94,999",
  hhi == 18 ~ "$95,000-$99,999",
  hhi == 19 ~ "$100,000-$124,999",
  hhi == 20 ~ "$125,000-$149,999",
  hhi == 21 ~ "$150,000-$174,999",
  hhi == 22 ~ "$175,000-$199,999",
  hhi == 23 ~ "$200,000-$249,999",
  hhi == 24 ~ "$250,000 and Above",
  hhi == 25 ~ "Prefer Not TO Answer"
))

#recode education 
subsetmodeldata <- subsetmodeldata %>% mutate(education, Education = case_when(
  education == 1 ~ "Some High School or Less",
  education == 2 ~ "High School (HS) Graduate",
  education == 3 ~ "Post HS Vocational School",
  education == 4 ~ "Some College, No Degree",
  education == 5 ~ "Associate's Degree",
  education == 6 ~ "Bachelor's Degree",
  education == 7 ~ "Master's or Professional Degree", 
  education == 8 ~ "Doctorate Degree"
))

#recode region variable
subsetmodeldata <- subsetmodeldata %>% mutate(region, Region = case_when(
  region == 1 ~ "Northeast", 
  region == 2 ~ "Midwest",
  region == 3 ~ "South", 
  region == 4 ~ "West",
  TRUE ~ NA_character_),
  Region = fct_relevel(Region, "South"))

#relevel factor for education
subsetmodeldata$Education <- factor(subsetmodeldata$Education, levels = c("Some High School or Less", "High School (HS) Graduate", "Post HS Vocational School", "Some College, No Degree", "Associate's Degree", "Bachelor's Degree", "Master's or Professional Degree", "Doctorate Degree"))

```


## Further Transformations

```{r}

#pivot data for mdoeling
subsetmodeldataLONG <- pivot_longer(subsetmodeldata, cols = c(Control, Dem_Treatment, GOP_Treatment),
                         names_to = "Treatment", values_to = "Supporting.Appointment")
####create DV####
subsetmodeldataLONG$Supporting.Appointment <- factor(subsetmodeldataLONG$Supporting.Appointment,
                                                     levels = c(1, 2, NA), labels = c("Yes", "No"))

subsetmodeldataLONG$Supporting.Appointment <- ifelse(subsetmodeldataLONG$Supporting.Appointment == "Yes", 1,
                                          ifelse(subsetmodeldataLONG$Supporting.Appointment == "No", 0, NA))

subsetmodeldataLONG <- subsetmodeldataLONG %>% mutate(Support.Appointment = factor(Supporting.Appointment, 
                                                                      levels = c(0, 1),
                                                                      labels = c("No", "Yes")))

subsetmodeldataLONG <- subset(subsetmodeldataLONG, !is.na(Supporting.Appointment))



### AFFECT VARIABLE ####
subsetmodeldataLONG <- subsetmodeldataLONG %>% 
  mutate(
    Affect = case_when(
      pid3lean == "Democrat"   ~ DemTherm - RepTherm,   # in-party – out-party
      pid3lean == "Republican" ~ RepTherm - DemTherm,
      pid3lean == "Independent" ~ RepTherm - DemTherm               
    )
  )


####create group variable#####
subsetmodeldataLONG$pid3lean <- factor(subsetmodeldataLONG$pid3lean, levels = c("Independent", "Democrat", "Republican"))


#Variable for in-group/out-group
subsetmodeldataLONG$inandout <-ifelse(subsetmodeldataLONG$pid3lean == "Independent", "Neither", #Independents included in Neither group 
                                ifelse(subsetmodeldataLONG$Treatment == "Dem_Treatment" & 
                                         subsetmodeldataLONG$pid3lean == "Democrat", "In-Party",
                                ifelse(subsetmodeldataLONG$Treatment == "GOP_Treatment" & 
                                         subsetmodeldataLONG$pid3lean == "Republican", "In-Party",
                                ifelse(subsetmodeldataLONG$Treatment == "Control", "Neither",
                                       "Out-Party"))))



subsetmodeldataLONG$inandout <- factor(subsetmodeldataLONG$inandout, 
                                       levels = c("Neither", "In-Party", "Out-Party"))

########

#relevel gender variable

subsetmodeldataLONG$gender <- factor(subsetmodeldataLONG$gender,
                                     levels = c(1,2),
                                     labels = c("male", "female"))

#clean out NA from group variable 
subsetmodeldataLONG <- subsetmodeldataLONG %>% drop_na(inandout)





```


## Renaming Variables (final)

```{r}

subsetmodeldataLONG <- subsetmodeldataLONG %>% dplyr::select(Treatment, Supporting.Appointment, pid3lean, age, inandout, Hispanic, Ethnicity, Education, HouseholdIncome, gender, DemTherm, RepTherm, TrustDem, TrustRep, Dem_dummy, GOP_dummy, control_dummy, Region, Affect) %>% rename(Party = pid3lean, Group = inandout, Age = age, Gender = gender, Supporting.Appointment = Supporting.Appointment)

```


## Summarise/Visualize!

```{r}
# kable(subsetmodeldataLONG)

sumfun <- dfSummary(subsetmodeldataLONG, varnumbers = FALSE,
          valid.col    = FALSE)

sumfun

```


## Distributions

```{r}

hist(subsetmodeldataLONG$Age)
hist(subsetmodeldataLONG$DemTherm)
hist(subsetmodeldataLONG$RepTherm)
hist(subsetmodeldataLONG$TrustDem)
hist(subsetmodeldataLONG$TrustRep)
ggplot(subsetmodeldataLONG, aes(x = Treatment)) +
  geom_bar()

ggplot(subsetmodeldataLONG, aes(x = Hispanic)) +
  geom_bar()

ggplot(subsetmodeldataLONG, aes(x = HouseholdIncome)) +
  geom_bar()

ggplot(subsetmodeldataLONG, aes(x = Education)) +
  geom_bar() +
  theme(axis.text.x = element_text(hjust = 1, angle = 45))

ggplot(subsetmodeldataLONG, aes(x = Party)) +
  geom_bar()

ggplot(subsetmodeldataLONG, aes(x = Ethnicity)) +
  geom_bar()
```

## Checking for Separation (dispersion among categorical variable gropus):

```{r}

table(subsetmodeldataLONG$Supporting.Appointment, subsetmodeldataLONG$GOP_dummy)    
table(subsetmodeldataLONG$Supporting.Appointment, subsetmodeldataLONG$Dem_dummy)
table(subsetmodeldataLONG$Supporting.Appointment, subsetmodeldataLONG$Gender)
table(subsetmodeldataLONG$Supporting.Appointment, subsetmodeldataLONG$Party)
table(subsetmodeldataLONG$Supporting.Appointment, subsetmodeldataLONG$Group)
table(subsetmodeldataLONG$Supporting.Appointment, subsetmodeldataLONG$Ethnicity)

#exclude Islander and Native American due to lack of occurences for Yes/no
table(subsetmodeldataLONG$Supporting.Appointment, subsetmodeldataLONG$Hispanic)
table(subsetmodeldataLONG$Supporting.Appointment, subsetmodeldataLONG$Education)
```


# Models 

```{r}
##in manuscript
partyonly <- glm(Supporting.Appointment ~  Party + Group,
             data = subsetmodeldataLONG, family = "binomial")
summary(partyonly)

  
```

```{r, warning=FALSE}

#both models here in main manuscript

partysocial <- glm(Supporting.Appointment ~  Party + Group + Gender + Affect, 
             data = subsetmodeldataLONG, family = "binomial")


#interaction of party and gender
partysocial_interact <- glm(Supporting.Appointment ~  Party + Group + Gender + Party*Gender + Affect, 
             data = subsetmodeldataLONG, family = "binomial")

 summary(partysocial)

 summary(partysocial_interact)

```


```{r}
# in supplemental materials

partysocial_race <- glm(Supporting.Appointment ~  Party + Group + Gender + Affect + Hispanic + Ethnicity, 
             data = subsetmodeldataLONG, family = "binomial")
summary(partysocial_race)

```


```{r}

#in supplemental materials 
partysocial_age <- glm(Supporting.Appointment ~  Party + Group + Gender + Age + Affect, 
             data = subsetmodeldataLONG, family = "binomial")
summary(partysocial_age)


```


```{r}

#in supplemental materials
partysocial_edu <- glm(Supporting.Appointment ~  Party + Group + Gender + Age + Education + Affect, 
             data = subsetmodeldataLONG, family = "binomial")
summary(partysocial_edu)

 #partysocial model + ethnicity and education  
```

```{r}

#income as numeric included in supplemental materials

#income as factor

partysocial_income <-  glm(Supporting.Appointment ~  Party + Group + Gender + Affect + HouseholdIncome, 
             data = subsetmodeldataLONG, family = "binomial")

#income as numeric
partysocial_incomenum <-  glm(Supporting.Appointment ~  Party + Group + Gender + Affect + as_numeric(HouseholdIncome), 
             data = subsetmodeldataLONG, family = "binomial")


```



```{r, warning=FALSE}

#in main manuscript
partysocial_trust <-  glm(Supporting.Appointment ~  Party + Group + Gender + TrustDem + TrustRep + Affect, 
             data = subsetmodeldataLONG, family = "binomial")

summary(partysocial_trust)


```

```{r}

model_allpredictors <- glm(Supporting.Appointment ~  Party + Group + Gender + TrustDem + TrustRep + DemTherm + RepTherm  + Education + Ethnicity + Hispanic, 
             data = subsetmodeldataLONG, family = "binomial")


summary(model_allpredictors)

```

## Visualize Results:

```{r}
### Run this after above modeling. Party factor is re-ordered for plotting.

custom_colors <- c("Democrat" = "blue", "Republican" = "red", "Independent" = "green")

subsetmodeldataLONG$Party <- factor(subsetmodeldataLONG$Party, levels = c("Independent","Democrat", "Republican"))

#simple rename -> changed language from party to group
subsetmodeldataLONG <- subsetmodeldataLONG %>% mutate(Group = case_when(
  Group == "Out-Party" ~ "Out-Group",
  Group == "In-Party" ~ "In-Group",
  Group == "Neither" ~ "Neither"
))

subsetmodeldataLONG$Group <- factor(subsetmodeldataLONG$Group, levels = c("Neither", "Out-Group", "In-Group"))




p1 <- plot_model(partysocial, type = "eff", terms = "Party") +
  theme_minimal()

 
### Graph in main manuscript
p2 <- plot_model(partysocial, type = "eff", terms = c("Group", "Party")) +
  scale_color_manual(values = custom_colors) +
  theme_minimal() +
  labs(title = "Probability of Appointment Support", y = "Probability (%)", 
       caption = "Predictors held at their highest proportion.") +
  theme(
    plot.caption.position = "plot",
    plot.caption = element_text(
      hjust = 1,
      vjust = -0.3,
      margin = margin(t = 6)
    )
  ) 

p2



p4 <- plot_model(partysocial_interact, type = "eff", terms = c("Party", "Gender")) +
  theme_minimal() 

p4





# ggsave(
#     filename = "coefficientplotsJA.jpeg",
#     plot = p3, 
#     device = "jpeg",
#     dpi = 300,
#     scale = 1.5,
#     height = 3,
#     width = 4.5,
#     units = "in"
# )
# 
# 
# ggsave(
#     filename = "party_groupME.jpeg",
#     plot = p2, 
#     device = "jpeg",
#     dpi = 300,
#     scale = 2.25,
#     height = 3,
#     width = 4.5,
#     units = "in"
# )




```

## Diagnostics - Balance Between Groups

```{r}

# both loveplots in supplemental materials

bal.tab(Treatment ~  Party + Gender + as_factor(HouseholdIncome) + Education, 
        data = subsetmodeldataLONG, un = TRUE)

p <- love.plot(Treatment ~  Party + Gender + as_factor(HouseholdIncome) + Education, data = subsetmodeldataLONG, abs = TRUE, 
          thresholds = .1, labels = FALSE,
          var.names = c(
    "Party_Independent" = "Independent",
    "Party_Democrat"    = "Democrat",
    "Party_Republican"  = "Republican",
    "Gender_female" = "Female",
    "as_factor(HouseholdIncome)_$100,000-$124,999" = "$100k–$124k",
    "as_factor(HouseholdIncome)_$125,000-$149,999" = "$125k–$149k",
    "as_factor(HouseholdIncome)_$15,000-$19,999"   = "$15k–$19k",
    "as_factor(HouseholdIncome)_$150,000-$174,999" = "$150k–$174k",
    "as_factor(HouseholdIncome)_$175,000-$199,999" = "$175k–$199k",
    "as_factor(HouseholdIncome)_$20,000-$24,999"   = "$20k–$24k",
    "as_factor(HouseholdIncome)_$200,000-$249,999" = "$200k–$249k",
    "as_factor(HouseholdIncome)_$25,000-$29,999"   = "$25k–$29k",
    "as_factor(HouseholdIncome)_$250,000 and Above"= "$250k+",
    "as_factor(HouseholdIncome)_$30,000-$34,999"   = "$30k–$35k",
    "as_factor(HouseholdIncome)_$35,000-$39,999"   = "$35k–$40k",
    "as_factor(HouseholdIncome)_$40,000-$44,999"   = "$40k–$45k",
    "as_factor(HouseholdIncome)_$45,000-$44,999"   = "$45k–$50k", 
    "as_factor(HouseholdIncome)_$50,000-$54,999"   = "$50k–$55k",
    "as_factor(HouseholdIncome)_$55,000-$59,999"   = "$55k–$60k",
    "as_factor(HouseholdIncome)_$60,000-$64,999"   = "$60k–$65k",
    "as_factor(HouseholdIncome)_$65,000-$69,999"   = "$65k–$70k",
    "as_factor(HouseholdIncome)_$70,000-$74,999"   = "$70k–$75k",
    "as_factor(HouseholdIncome)_$75,000-$79,999"   = "$75k–$80k",
    "as_factor(HouseholdIncome)_$80,000-$84,999"   = "$80k–$85k",
    "as_factor(HouseholdIncome)_$85,000-$89,999"   = "$85k–$90k",
    "as_factor(HouseholdIncome)_$90,000-$94,999"   = "$90k–$95k",
    "as_factor(HouseholdIncome)_$95,000-$99,999"   = "$95k–$100k",
    "as_factor(HouseholdIncome)_Less than $14,999" = "< $15k",
    "Education_Some High School or Less"      = "≤ High School",
    "Education_High School (HS) Graduate"     = "HS Graduate",
    "Education_Post HS Vocational School"     = "Vocational",
    "Education_Some College, No Degree"       = "Some College",
    "Education_Associate's Degree"            = "Associate’s",
    "Education_Bachelor's Degree"             = "Bachelor’s",
    "Education_Master's or Professional Degree" = "Master’s/Professional",
    "Education_Doctorate Degree"              = "Doctorate"
  )
)

p +   theme(
    text = element_text(size = 14),
    axis.text.y = element_text(size = 6.5)
    
  )

p

# ggsave(
#     filename = "balance.jpeg",
#     plot = p, 
#     device = "jpeg",
#     dpi = 300,
#     scale = 2,
#     height = 3,
#     width = 4.5,
#     units = "in"
# )


lp2 <- love.plot(Treatment ~ as_numeric(HouseholdIncome) + Ethnicity + Hispanic + Age, data = subsetmodeldataLONG, abs = TRUE, 
          thresholds = .1, stars = "std", drop.missing = TRUE,
          var.names = c(
            "as.numeric(HouseholdIncome_num)" = "Numeric Household Income",
            "Ethnicity_White" = "White",
            "Ethnicity_American Indian/Alaskan Native" = "Native American/Alaskan Native",
            "Ethnicity_Asian" = "Asian",
            "Ethnicity_Pacific Islander" = "Pacific Islander",
            "Ethnicity_Other" = "Other Ethnicity",
            "Ethnicity:<NA>" = "NA Ethnicity",
            "Hispanic_Hispanic" = "Hispanic"
          ))

lp2




# ggsave(
#     filename = "balance_small.jpeg",
#     plot = lp2, 
#     device = "jpeg",
#     dpi = 300,
#     scale = 2,
#     height = 3,
#     width = 4.5,
#     units = "in"
# )



#-------------------------------------------------------------------------------------------------------


```


# Making Tables
```{r}

#in main manuscript
stargazer(partyonly, partysocial, partysocial_interact, partysocial_trust, title="Results", 
          dep.var.labels = "Propensity to Answer Yes",no.space = TRUE, column.sep.width = "5pt", 
          type = "latex", out = "JAmodels.txt",
          single.row = TRUE)


##### For APPENDIX ######----------------------------------------------------------------------------

stargazer(partysocial_income, partysocial_age, partysocial_race, partysocial_edu, title="Results",
          dep.var.labels = "Propensity to Answer Yes",no.space = TRUE, column.sep.width = "5pt", 
          type = "latex", out = "JAmodels.txt",
          single.row = TRUE)

#----------------------------------------------------------------------------------------------------


```


# Diagnostics Regression:

```{r}
#Multicolinearity Check
# vifcheck <- vif(partyonly)
# vifcheck
# 
# vifcheck2 <- vif(partysocial)
# vifcheck2
# 
# vif(partysocial_interact, type = "predictor")
# vifcheck3 <- vif(partysocialethn)
# vifcheck3
# 
# vifcheck4 <- vif(partysocialedu)
# vifcheck4
# 
# vifcheck5 <- vif(partysocial_feelings)
# vifcheck5
# 
# vifcheck6 <- vif(partysocial_incomeedu)
# vifcheck6
# 
# vifcheck7 <- vif(partysocial_trust)
# vifcheck7
```

```{r}
dwt(partysocial)

dwt(partyonly)

dwt(partysocial_trust)
```


```{r}

# component residual plots

car::crPlots(partysocial_feelings2, terms = ~ Affect,
             pch=20, col="gray",
             smooth = list(smoother=car::gamLine))

car::crPlots(partysocial_edu, terms = ~ as.numeric(Education))

car::crPlots(partysocial_income, terms = ~ as.numeric(HouseholdIncome_num),
             col="gray") +
  theme(axis.title.x = "Household Income")

car::crPlots(partysocial_trust)

# multiple diagnostic plots

plot(partysocial)

plot(partysocial_interact)

plot(partyonly)

plot(partysocial_trust)

```

```{r}
#Goodness of Fit tests

hltest(partysocial, verbose = TRUE)

hltest(partysocial_trust, verbose = TRUE)

hltest(partysocialethn, verbose = TRUE)

hltest(partysocialedu, verbose = TRUE)

hltest(partysocial_ethedu, verbose = TRUE)



```



# Marginal Effects for Partysocial Model 

```{r}
library(margins)

probabilities <- plogis(coef(partysocial))
probabilities

marg_eff <- margins(partysocial, data = model.frame(partysocial))

marg_table <- summary(marg_eff)

marg_table$AME <- marg_table$AME*100

marg_table <- marg_table %>% 
  select(factor, AME, SE, p)

# stargazer(marg_table, summary = FALSE, 
#           align = TRUE,
#           no.space = TRUE,
#           style = "apsr")

```




## Equality of Coefficients

```{r}

equalcoef1 <- linearHypothesis(partyonly, "PartyDemocrat = PartyRepublican")

equalcoef2 <- linearHypothesis(partysocial, "PartyDemocrat = PartyRepublican")

equalcoef3 <- linearHypothesis(partysocial_interact, "PartyDemocrat = PartyRepublican")

equalcoef4 <- linearHypothesis(partysocial_trust, "PartyDemocrat = PartyRepublican")

stargazer(equalcoef1, equalcoef2, equalcoef3, equalcoef4, type = "text")

```

